import Promise from 'bluebird';

import service from '../service/login';
import CONSTANT from '../constant/login';

const postLogin = ({ commit }, data) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.postLogin(data).then((res) => {
      commit(CONSTANT.LOGIN_SUCCESS, res);
      return resolve(res);
    }, (e) => {
      commit(CONSTANT.LOGIN_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

const postPassword = ({ commit }, data) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.postPassword(data).then((res) => {
      commit(CONSTANT.SET_PASSWORD_SUCCESS, res);
      return resolve(res);
    }, (e) => {
      commit(CONSTANT.SET_PASSWORD_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

const getLoginInfo = ({ commit }, data) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.getLoginInfo(data).then((res) => {
      commit(CONSTANT.GET_LOGIN_INFO, res);
      return resolve(res);
    }, e => reject(e));
    return server;
  });
  return ret;
};

const setLoginInfo = ({ commit }, info) => {
  commit(CONSTANT.SET_LOGIN_INFO, info);
};

export default {
  postLogin,
  postPassword,
  getLoginInfo,
  setLoginInfo,
};
